package cn.sczc.jh.client.service.team;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczc.jh.client.entity.Teams;
import cn.sczc.jh.client.exception.BusinessException;
import cn.sczc.jh.client.mapper.team.TeamMapper;
import cn.sczc.jh.client.util.CoordTranform;
import cn.sczc.jh.client.vo.MapFeaturesVo;
import cn.sczc.jh.client.vo.MapGeometryVo;
import cn.sczc.jh.client.vo.MapPropertiesVo;
import cn.sczc.jh.client.vo.MapVo;

@Transactional(rollbackFor = Exception.class)
@Service
public class TeamService extends ServiceImpl<TeamMapper, Teams> {

	@Autowired
	private TeamMapper teamMapper;

	public Page<Teams> queryTeams(Page<Teams> page, Map<String, Object> params) {
		List<Teams> teams = teamMapper.queryTeams(page, params);
		page.setRecords(teams);
		return page;
	}

	public int insert(Teams team) {
		QueryWrapper<Teams> wrapper = new QueryWrapper<>();
		wrapper.eq("name", team.getName());
		wrapper.eq("tenant_id", team.getTenantId());
		int count = teamMapper.selectCount(wrapper);
		if (count > 0) {
			throw new BusinessException("井队已存在！");
		}
		if (!"".equals(team.getLonLat())) {
			String[] lonLat = team.getLonLat().split(",");
			team.setLon(Double.valueOf(lonLat[0]));
			team.setLat(Double.valueOf(lonLat[1]));
		}
		if (team.getOid() == null) {
			team.setOid(UUID.randomUUID().toString());
		}
		team.setCreateTime(new Date());
		return teamMapper.insert(team);
	}

	public int update(Teams team) {
		QueryWrapper<Teams> wrapper = new QueryWrapper<>();
		wrapper.ne("oid", team.getOid());
		wrapper.eq("name", team.getName());
		wrapper.eq("tenant_id", team.getTenantId());
		int count = teamMapper.selectCount(wrapper);
		if (count > 0) {
			throw new BusinessException("井队已存在！");
		}
		if (!"".equals(team.getLonLat())) {
			String[] lonLat = team.getLonLat().split(",");
			team.setLon(Double.valueOf(lonLat[0]));
			team.setLat(Double.valueOf(lonLat[1]));
		}
		team.setUpdateTime(new Date());
		return teamMapper.updateById(team);
	}

	public int delById(String id) {
		return teamMapper.deleteById(id);
	}

	public MapVo qryClusterList(Map<String, Object> params) {
		MapVo mapvo = null;
		List<Teams> teams = teamMapper.selectListByParams(params);
		if (teams != null && teams.size() > 0) {
			mapvo = new MapVo();
			mapvo.setType("FeatureCollection");
			List<MapFeaturesVo> mapFeaturesVo = new ArrayList<MapFeaturesVo>();
			for (Teams team : teams) {
				if (team.getLon() != null && team.getLat() != null) {
					double[] coord = { team.getLon(), team.getLat() };
					double[] point = CoordTranform.lnglat2mercator(coord);
					MapGeometryVo geometry = new MapGeometryVo();
					List<Object> coordinates = new ArrayList<Object>();
					coordinates.add(point[0]);
					coordinates.add(point[1]);
					geometry.setCoordinates(coordinates);
					geometry.setType("Point");

					MapPropertiesVo properties = new MapPropertiesVo();
					properties.setCatecd("zddw");
					properties.setPoint(point[0] + "_" + point[1]);
					properties.setId(team.getOid());
					properties.setName(team.getName());

					MapFeaturesVo features = new MapFeaturesVo();
					features.setGeometry(geometry);
					features.setProperties(properties);
					features.setType("Feature");
					features.setId(team.getOid());
					mapFeaturesVo.add(features);
				}
			}
			mapvo.setFeatures(mapFeaturesVo);
		}
		return mapvo;
	}

	public List<Teams> selectListByTenantId(Long tenantId) {
		QueryWrapper<Teams> wrapper = new QueryWrapper<>();
		wrapper.eq("tenant_id", tenantId);
		return teamMapper.selectList(wrapper);
	}

}
